package com.jf.car.dao.impl;

import com.jf.car.bean.Apply;
import com.jf.car.dao.ApplyDao;
import com.jf.car.util.BaseDao;
import com.jf.car.util.MyUtils;
import com.jf.car.util.PageInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ApplyDaoImpl implements ApplyDao {
    @Override
    public List<Apply> getAll() {
        String sql = "select * from apply";
        return BaseDao.baseQuery(Apply.class,sql);
    }

    @Override
    public List<Apply> getPage(PageInfo pageInfo, Apply apply) {
        String sql = "select * from apply order by id desc limit ?,?";
        List<Object> objs = new ArrayList<>();
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Apply.class,sql,objs);
    }

    @Override
    public int updateSelect(Apply apply) {
        String sql = "update apply set ";
        List<Object> objs = new ArrayList<>();
        if (apply.getApplyId() != 0){
            sql+=" apply_id=?,";
            objs.add(apply.getApplyId());
        }
        if (apply.getApplyTime() != null){
            sql+=" apply_time=?,";
            objs.add(apply.getApplyTime());
        }
        if (apply.getApplyCarId() != 0){
            sql+=" apply_car_id=?,";
            objs.add(apply.getApplyCarId());
        }
        if (apply.getApplyDriver() != 0){
            sql+=" apply_driver=?,";
            objs.add(apply.getApplyDriver());
        }
        if (apply.getApplyDriverId() != 0){
            sql+=" apply_driver_id=?,";
            objs.add(apply.getApplyDriverId());
        }
        if (MyUtils.isStringNotEmpty(apply.getContent())){
            sql+=" content=?,";
            objs.add(apply.getContent());
        }
        if (apply.getDepExamId() != 0){
            sql+=" dep_exam_id=?,";
            objs.add(apply.getDepExamId());
        }
        if (apply.getDepExamTime() != null){
            sql+=" dep_exam_time=?,";
            objs.add(apply.getDepExamTime());
        }
        if (MyUtils.isStringNotEmpty(apply.getDepContent())){
            sql+= " dep_content=?,";
            objs.add(apply.getDepContent());
        }
        if (apply.getAdmExamId() != 0){
            sql+=" adm_exam_id=?,";
            objs.add(apply.getAdmExamId());
        }
        if (apply.getAdmExamTime() != null){
            sql+=" adm_exam_time=?,";
            objs.add(apply.getAdmExamTime());
        }
        if (MyUtils.isStringNotEmpty(apply.getAdmExamContent())){
            sql+=" adm_exam_content=?,";
            objs.add(apply.getAdmExamContent());
        }
        if (apply.getCarId() != 0){
            sql+=" car_id=?,";
            objs.add(apply.getCarId());
        }
        if (apply.getDriverId() !=0){
            sql+=" driver_id=?,";
            objs.add(apply.getDriverId());
        }
        if (apply.getApplyStatus()!=0){
            sql+=" apply_status=?,";
            objs.add(apply.getApplyStatus());
        }
        if (apply.getStartTime() != null){
            sql+=" start_time=?,";
            objs.add(apply.getStartTime());
        }
        if (apply.getEndTime() != null){
            sql+=" end_time=?,";
            objs.add(apply.getEndTime());
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(apply.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int getCount(Apply apply) {
        String sql = "select count(id) from apply";
        return BaseDao.baseQueryCount(sql);
    }

    @Override
    public Apply getById(int id) {
        String sql = "select * from apply where id = ?";
        return BaseDao.baseQueryBean(Apply.class,sql,id);
    }
}
